﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFMediaKit.DirectShow.MediaPlayers;
using VideoClient.Utils;
using System.Threading;
using System.Diagnostics;
using NTheora;
using VideoClient.VideoConferencing;
using System.ServiceModel;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace VideoClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [CallbackBehavior(UseSynchronizationContext = false)]
    public partial class MainWindow : Window, IVideoConferenceServiceCallback
    {
        private NTheora.Encoder encoder;
        private NTheora.Decoder decoder;
        private VideoConferenceServiceClient videoClient;
        private byte[] bitmapData;
        private TheoraPacket[] encodedImage = new TheoraPacket[10];
        private YCbCrImage decodedImage;
        
        public MainWindow()
        {
            InitializeComponent();

            encoder = new NTheora.Encoder(this.videoElement.DesiredPixelWidth, this.videoElement.DesiredPixelHeight);
            decoder = new NTheora.Decoder(this.videoElement.DesiredPixelWidth, this.videoElement.DesiredPixelHeight, encoder.GetHeader());

            decodedImage = new NTheora.YCbCrImage(this.videoElement.DesiredPixelWidth, this.videoElement.DesiredPixelHeight);

            videoClient = new VideoConferenceServiceClient(new InstanceContext(this), "NetTcpBinding_IVideoConferenceService");
            videoClient.Subscribe();
        }

        private void VideoCapture_NewVideoSample(object sender, VideoSampleArgs e)
        {
            NTheora.YCbCrImage ycbcrImage;
            if (e.Format == VideoPixelFormat.I420)
                ycbcrImage = NTheora.YCbCrImage.CreateFromI420(e.Frame, e.Width, e.Height);
            else
                ycbcrImage = NTheora.YCbCrImage.CreateFromRGB24(e.Frame, e.Width, e.Height);
                
            var result = encoder.Encode(ycbcrImage, encodedImage);
            if (result == 0)
                return;

            var frameData = encodedImage[0].ToArray();
            if (frameData.Length > 0)
                videoClient.Publish(frameData);

            //Console.WriteLine("Ratio: " + encodedImage[0].GetCount() + " / " + e.Width * e.Height * 3);
        }

        public void OnData(byte[] data)
        {
            decoder.Decode(new TheoraPacket(data), decodedImage);
            if (bitmapData == null)
                bitmapData = new byte[3 * decodedImage.Width * decodedImage.Height];

            decodedImage.ToRGB(bitmapData);

            receivedImage.Dispatcher.Invoke(new ThreadStart(() =>
            {
                receivedImage.Source = BitmapSource.Create(decodedImage.Width, decodedImage.Height, 96, 96, PixelFormats.Rgb24, BitmapPalettes.Halftone256, bitmapData, 3 * decodedImage.Width);
            }));
        }
    }
}
